package com.augurit.gzcgwsp.wfex.bi.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.gzcgwsp.wfex.bi.service.IWfBusTemplateServiceEX;
import com.augurit.gzcgwsp.wfex.bi.service.IWfTemplateAttachmentRefServiceEX;
import com.augurit.gzcgwsp.wfex.bi.web.form.WfTemplateAttachmentRefFormEX;
import com.augurit.wf.bi.convert.WfBusTemplateConvertor;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;
import com.augurit.wf.common.dao.WfBusTemplateDao;
import com.augurit.wf.common.entity.WfBusTemplate;


@Service
@Transactional
public class WfBusTemplateServiceImplEX implements IWfBusTemplateServiceEX{

	@Autowired
	private IWfTemplateAttachmentRefServiceEX wfTemplateAttachmentRefServiceEX;
	
	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	
	@Autowired
	private WfBusTemplateDao wfBusTemplateDao;
	
	public IWfBusTemplateService getWfBusTemplateService(){
		return this.wfBusTemplateService;
	}
	
	 /**
     * 获取打印模板
     * @param templateCode
     * @return
     */
    public String getPrintTemplatePath(Long id){
        return wfTemplateAttachmentRefServiceEX.getPrintTemplatePath(id);
    }
    
    /**
     * 获取打印模板
     * @param templateCode
     * @return
     */
    public String getPrintTemplatePath(String templateCode, String fileType){
        return wfTemplateAttachmentRefServiceEX.getPrintTemplatePath(templateCode, fileType);
    }
    
    @Transactional(readOnly = true)
	public List<WfBusTemplateForm> getAllActive(LoginUserForm userForm){
    	StringBuffer sb = new StringBuffer();
    	sb.append("select distinct template.* from Wf_Bus_Template template, Wf_Bus_Template_Type type where template.template_Type_Id=type.id");
    	sb.append(" and template.is_Active='1' ");
//    	TODO 燃气收案只看到类型为燃气的，以此类推
    	Long[] roleIds = userForm.getRoleIds();
    	boolean ifHasPermission = false;//是否有收案权限（目前只有窗口人员有收案权限）
    	//获取角色配置
    	Map<String,String> roleIdsMap = this.getRoleIds();
    	boolean sRqc = false;//是否有   市燃气处   收案权限
    	boolean qRqc = false;//是否有   区燃气处   收案权限
    	
    	boolean sYnc = false;//是否有   市余泥处   收案权限
    	boolean qYnc = false;//是否有   区余泥处   收案权限
    	
    	boolean sSrc = false;//是否有   市市容处   收案权限
    	boolean qSrc = false;//是否有   区市容处   收案权限
    	
    	boolean sJgc = false;//是否有   市景观处   收案权限
    	boolean qJgc = false;//是否有   区景观处   收案权限
    	
    	boolean sSsc = false;//是否有   市设施处   收案权限
    	boolean qSsc = false;//是否有   区设施处   收案权限
    	
    	boolean qHwcLy = false;//是否有   区环卫处   收案权限
    	boolean qHwcSy = false;//是否有   区环卫处   收案权限
    	boolean qHwcFqw = false;//是否有   区环卫处   收案权限
    	
    	boolean flag = false;//是否已经有至少一个权限的标志，用于判定sql是用and还是用or
    	
    	if(roleIds != null && roleIds.length > 0){
    		for(int i=0;i<roleIds.length;i++){
    			if(roleIdsMap.get("rqcIds").contains(roleIds[i].toString())){//燃气处
    				sRqc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("quRqcIds").contains(roleIds[i].toString())){//区燃气处
    				qRqc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("yncIds").contains(roleIds[i].toString())){//余泥处
    				sYnc = true;
    				ifHasPermission = true;
    			} 
    			if(roleIdsMap.get("quyncIds").contains(roleIds[i].toString())){//区余泥处
    				qYnc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("jgcIds").contains(roleIds[i].toString())){//景观处
    				sJgc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("srcIds").contains(roleIds[i].toString())){//市容处
    				sSrc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("quSrcIds").contains(roleIds[i].toString())){//区市容处
    				qSrc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("sscIds").contains(roleIds[i].toString())){//设施处
    				sSsc = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("hwcLyIds").contains(roleIds[i].toString())){//环卫处-陆域
    				qHwcLy = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("hwcSyIds").contains(roleIds[i].toString())){//环卫处-水域
    				qHwcSy = true;
    				ifHasPermission = true;
    			}
    			if(roleIdsMap.get("hwcFqwIds").contains(roleIds[i].toString())){//环卫处-废弃物中心
    				qHwcFqw = true;
    				ifHasPermission = true;
    			}
    		}    		
    	}
    	
    	
    	if(flag){
    		if(sRqc == true && qRqc == true){//有燃气处市和区的权限
        		sb.append(" or template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='7') ");
        	}else if(sRqc == true && qRqc == false){//有市，没区的权限
        		sb.append(" or template.id in (select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='7' and wbt.template_Code not like '%qx%') ");
        	}else if(sRqc == false && qRqc == true){//有区，没市的权限
        		sb.append(" or template.id in ( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='7' and wbt.template_Code like '%qx%') ");
        	}
    	}else{
    		if(sRqc == true && qRqc == true){//有燃气处市和区的权限
        		sb.append(" and template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='7') ");
        		flag = true;
        	}else if(sRqc == true && qRqc == false){//有市，没区的权限
        		sb.append(" and template.id in (select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='7' and wbt.template_Code not like '%qx%') ");
        		flag = true;
        	}else if(sRqc == false && qRqc == true){//有区，没市的权限
        		sb.append(" and template.id in ( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='7' and wbt.template_Code like '%qx%') ");
        		flag = true;
        	}
    		
    	}
    	
    	if(flag){
    		if(sYnc == true && qYnc == true){//有余泥处处市和区的权限
        		sb.append(" or template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='9') ");
        	}else if(sYnc == true && qYnc == false){//有市，没区的权限
        		sb.append(" or template.id in (select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='9' and wbt.template_Code not like '%qx%') ");
        	}else if(sYnc == false && qYnc == true){//有区，没市的权限
        		sb.append(" or template.id in ( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='9' and wbt.template_Code like '%qx%') ");
        	}
    	}else{
    		if(sYnc == true && qYnc == true){//有余泥处市和区的权限
        		sb.append(" and template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='9') ");
        		flag = true;
        	}else if(sYnc == true && qYnc == false){//有市，没区的权限
        		sb.append(" and template.id in (select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='9' and wbt.template_Code not like '%qx%') ");
        		flag = true;
        	}else if(sYnc == false && qYnc == true){//有区，没市的权限
        		sb.append(" and template.id in ( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='9' and wbt.template_Code like '%qx%') ");
        		flag = true;
        	}
    	}
    	
    	if(flag){
    		if(sSrc == true && qSrc == true){//有市容处市和区的权限
        		sb.append(" or template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='13') ");
        	}else if(sSrc == true && qSrc == false){//有市，没区的权限
        		sb.append(" or template.id in (select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='13' and wbt.template_Code not like '%qx%') ");
        	}else if(sSrc == false && qSrc == true){//有区，没市的权限
        		sb.append(" or template.id in ( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='13' and wbt.template_Code like '%qx%') ");
        	}
    	}else{
    		if(sSrc == true && qSrc == true){//有市容处市和区的权限
        		sb.append(" and template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='13') ");
        		flag = true;
        	}else if(sSrc == true && qSrc == false){//有市，没区的权限
        		sb.append(" and template.id in (select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='13' and wbt.template_Code not like '%qx%') ");
        		flag = true;
        	}else if(sSrc == false && qSrc == true){//有区，没市的权限
        		sb.append(" and template.id in ( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='13' and wbt.template_Code like '%qx%') ");
        		flag = true;
        	}
    	}
    	
    	if(flag){
    		if(sJgc == true && qJgc == true){//有景观处市和区的权限
        		sb.append(" or template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='11') ");
        	}else if(sJgc == true && qJgc == false){//有市，没区的权限
        		sb.append(" or template.id in (select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='11' and wbt.template_Code not like '%qx%') ");
        	}else if(sJgc == false && qJgc == true){//有区，没市的权限
        		sb.append(" or template.id in ( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='11' and wbt.template_Code like '%qx%') ");
        	}
    	}else{
    		if(sJgc == true && qJgc == true){//有景观处市和区的权限
        		sb.append(" and template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='11') ");
        		flag = true;
        	}else if(sJgc == true && qJgc == false){//有市，没区的权限
        		sb.append(" and template.id in (select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='11' and wbt.template_Code not like '%qx%') ");
        		flag = true;
        	}else if(sJgc == false && qJgc == true){//有区，没市的权限
        		sb.append(" and template.id in ( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='11' and wbt.template_Code like '%qx%') ");
        		flag = true;
        	}
    	}
    	
    	if(flag){
    		if(sSsc == true && qSsc == true){//有设施处市和区的权限
        		sb.append(" or template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='14') ");
        	}else if(sSsc == true && qSsc == false){//有市，没区的权限
        		sb.append(" or template.id in (select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='14' and wbt.template_Code not like '%qx%') ");
        	}else if(sSsc == false && qSsc == true){//有区，没市的权限
        		sb.append(" or template.id in ( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='14' and wbt.template_Code like '%qx%') ");
        	}
    	}else{
    		if(sSsc == true && qSsc == true){//有设施处市和区的权限
        		sb.append(" and template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='14') ");
        		flag = true;
        	}else if(sSsc == true && qSsc == false){//有市，没区的权限
        		sb.append(" and template.id in (select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='14' and wbt.template_Code not like '%qx%') ");
        		flag = true;
        	}else if(sSsc == false && qSsc == true){//有区，没市的权限
        		sb.append(" and template.id in ( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='14' and wbt.template_Code like '%qx%') ");
        		flag = true;
        	}
    	}
    	
    	if(flag){
    		if(qHwcLy == true){//有区环卫处陆域的权限
        		sb.append(" or template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='15' and wbt.template_Code like '%shljqssj%') ");
        	}
    	}else{
    		if(qHwcLy == true){//有区环卫处陆域的权限
        		sb.append(" and template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='15' and wbt.template_Code like '%shljqssj%') ");
        		flag = true;
        	}
    	}
    	
    	if(flag){
    		if(qHwcSy == true){//有区环卫处水域的权限
        		sb.append(" or template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='15' and (wbt.template_Code='shljys' or wbt.template_Code='shljsyba')) ");
        	}
    	}else{
    		if(qHwcSy == true){//有区环卫处水域的权限
        		sb.append(" and template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='15' and (wbt.template_Code='shljys' or wbt.template_Code='shljsyba')) ");
        		flag = true;
    		}
    	}
    	
    	if(flag){
    		if(qHwcFqw == true){//有区环卫处废弃物的权限
        		sb.append(" or template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='15' and wbt.template_Code='shljcz' ) ");
        	}
    	}else{
    		if(qHwcFqw == true){//有区环卫处废弃物的权限
        		sb.append(" and template.id in( select wbt.id from wf_Bus_Template wbt, wf_Bus_Template_Type wbtt ");
        		sb.append("where  wbt.template_Type_Id = wbtt.id and wbt.is_Active = '1' and wbt.template_Type_Id='15' and wbt.template_Code='shljcz') ");
        		flag = true;
    		}
    	}
    	
    	if(ifHasPermission == false || !flag){//如果没有收案权限（目前只有窗口人员有收案权限），则返回null
    		return null;
    	}
    	
    	
    	sb.append("order by template.template_Type_Id");
//		List list = wfBusTemplateDao.find(sb.toString(), OrgConstant.ONE);
    	SQLQuery query = wfBusTemplateDao.getSession().createSQLQuery(sb.toString()).addEntity(WfBusTemplate.class);
    	List<WfBusTemplate> list = query.list();
		if(list != null && list.size() > 0){
			Map<String,String> idAndTemplateTypeNameMap = getIdAndTemplateTypeNameMap();
			List<WfBusTemplateForm> result = new ArrayList<WfBusTemplateForm>();			
			for(int i =0;i<list.size();i++){
				WfBusTemplateForm form = WfBusTemplateConvertor.convertVoToForm((WfBusTemplate)list.get(i));
				form.setTemplateTypeName((String)idAndTemplateTypeNameMap.get(""+form.getTemplateTypeId()));
				result.add(form);
			}
			return result;
		}
		return null;
	}
    
    /**
     * 获取角色配置(窗口，只有窗口才能收案)
     * @return
     */
    public Map<String,String> getRoleIds(){
    	Map<String,String> roleIdsMap = new HashMap<String,String>();
    	roleIdsMap.put("rqcIds", "63");//市燃气处--窗口
    	roleIdsMap.put("quRqcIds", "137");//区燃气处窗口
    	
    	roleIdsMap.put("yncIds", "68");//市余泥处--窗口
    	roleIdsMap.put("quyncIds", "126");//区排放消纳窗口
    	
    	roleIdsMap.put("jgcIds", "80");//市景观处_窗口
    	
    	roleIdsMap.put("srcIds", "85");//市市容处_窗口
    	roleIdsMap.put("quSrcIds", "146");//区市容处窗口
    	
    	roleIdsMap.put("sscIds", "90");//市设施处_窗口
    	
    	roleIdsMap.put("hwcLyIds", "110");//环卫处  陆域—窗口
    	roleIdsMap.put("hwcSyIds", "112");//环卫处  水域—窗口
    	roleIdsMap.put("hwcFqwIds", "113");//环卫处  废弃物中心—窗口
    	return roleIdsMap;    	
    }
    
    /**
     * 获取业务类型,在WF_BUS_TEMPLATE_TYPE表中
     * @return
     */
    public Map<String,String> getIdAndTemplateTypeNameMap(){
    	Map<String,String> idAndTemplateTypeNameMap = new HashMap<String,String>();
    	idAndTemplateTypeNameMap.put("7", "燃气");
    	idAndTemplateTypeNameMap.put("9", "余泥");    	
    	idAndTemplateTypeNameMap.put("11", "景观");    	
    	idAndTemplateTypeNameMap.put("13", "市容");
    	idAndTemplateTypeNameMap.put("14", "设施");    	
    	idAndTemplateTypeNameMap.put("15", "环卫");
    	return idAndTemplateTypeNameMap;    	
    }

    /*
     * (non-Javadoc)
     * @see com.augurit.gzcgwsp.wfex.bi.service.IWfBusTemplateServiceEX#getEntityByCode(java.lang.String)
     */
	@Override
	public WfBusTemplate getEntityByCode(String templateCode) {
		String sql=" from WfBusTemplate where templateCode='"+templateCode+"'";
		return this.wfBusTemplateDao.findUnique(sql);
	}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.wfex.bi.service.IWfBusTemplateServiceEX#getRedFile(java.lang.String)
	 */
	@Override
	public String getRedFile(String templateCode) {
		if(templateCode.contains("qx")){
			templateCode = templateCode.replace("qx", "");
		}
		Long tempId=getEntityByCode(templateCode).getId();
		System.out.println("==== "+tempId);
		WfTemplateAttachmentRefFormEX form=wfTemplateAttachmentRefServiceEX.getRedFile(tempId);
		return form.getFilePath()+"/"+form.getFileRealName();
	}
}
